<?php
 require_once ("include.php");
 require_once ("registereduserobj.php");
 
 if (isset($_REQUEST['pw1']) && isset($_REQUEST['pw2']) && isset($_REQUEST['id']) && isset($_REQUEST['u'])) {
  if (validpassword($_REQUEST['pw1'])) {
   echo dothereset();    
	} else {
	 echo "That is not a valid password";
	 echo resetpassword();
	}
 } elseif (isset($_REQUEST['id']) && isset($_REQUEST['u'])) {
  echo resetpassword(); 
 } else {
  echo "IP Logged. Please do not visit this page again";
 }
?>

<?php
 function dothereset() {
  $id = $_REQUEST['id'] ;
	$email = $_REQUEST['u'];
	$pw1 = $_REQUEST['pw1'];
	if (filter_var($email,FILTER_VALIDATE_EMAIL)) {
   $obj = new registereduser();
   if ($obj->readuser($email)) {
	  if ($obj->header[7] == $id) {
		 $obj->header[7] = md5(time()) ;
		 $obj->header[5] = md5($pw1);
		 $obj->writedb();
		 $o = '<a href = "/index.php">Password now reset. Click to retry</a>'; // Take back to logon screen.
		} else {
		 $o = '1. Could not reset your password';
		}
	 } else {
	  $o = '2. Could not reset your password';
	 }
	} else {
	 $o = '3. Could not reset your password';	
	}
	return $o;
 }
?>

<?php
 function resetpassword() {
  $id = $_REQUEST['id'] ;
	$email = $_REQUEST['u'];
	if (filter_var($email,FILTER_VALIDATE_EMAIL)) {
   $obj = new registereduser();
   if ($obj->readuser($email)) {
	  if ($obj->header[7] == $id) {
		 $o = "
<html>
 <script>
  function cmp() {
	 if (document.getElementById('pw1').value == document.getElementById('pw2').value 
	  && document.getElementById('pw1').value != ''
	  && document.getElementById('pw1').value.length > 4
		) {
	  document.getElementById('sub').disabled=false;
	 } else {
	  document.getElementById('sub').disabled=true;
	 }
	}
 </script>
 <body onload='document.form.pw1.focus()'>
 <form name=form method=post>
 <input type=hidden name=id value='$id'>
 <input type=hidden name=u value='$email'> 
 <table>
	<tr>
	 <td>Enter Password</td>
	 <td><input onKeyup=cmp() id=pw1 type=password name=pw1></td>
	</tr>
	<tr>
	 <td>Reenter Password</td>
	 <td><input onKeyup=cmp() id=pw2 type=password name=pw2></td>
	</tr>
 </table>
 <input id=sub name=sub type=submit disabled=true>
 </form>
</html>
";
		} else {
		 $o = '1. There is a problem with your visit. Please close your browser';
		}
	 } else {
	  $o = '2. There is a problem with your visit. Please close your browser';
	 }
	} else {
   $o = '3. There is a problem with your visit. Please close your browser';
	}


	return $o;
 }
?>